//
//  MineHead.swift
//  GuMro
//
//  Created by mac on 2019/12/5.
//  Copyright © 2019 cn.gomro.app. All rights reserved.
//

import UIKit

/** 用户中心顶部信息 */
class MineHead: UIView {

    /** 查看用户信息 */
    var showUserDetailsBlock:(()->(Void))?
    
    /** 去登录 */
    var gotoLoginBlock:(()->(Void))?
    
    static let mine_head_height:CGFloat = 92
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        
        self.bgImg.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.edges.equalTo(self)
        }
        
        self.labName.snp.makeConstraints { make in
            make.width.equalTo(200)
            make.height.equalTo(21)
            make.left.equalTo(11)
            make.top.equalTo(19)
        }
        
        self.imgUserIcon.snp.makeConstraints { make in
            make.width.height.equalTo(60)
            make.right.equalTo(-40.5)
            make.top.equalTo(14.5)
        }
        
        self.labCompany.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            
            make.right.equalTo(self.imgUserIcon.snp.left).offset(11)
            make.height.equalTo(21)
            make.left.equalTo(self.labName.snp.left)
            make.top.equalTo(self.labName.snp.bottom).offset(10.5)
        }
        
        self.btnRight.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            
            make.width.height.equalTo(40)
            make.right.equalTo(0)
            make.centerY.equalTo(self.snp.centerY)
        }
    }

    
    //MARK: - initView
    private func initView(){
        
        //点击登录
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(togoLogin(sender:)))
        self.addGestureRecognizer(tap)
        self.isUserInteractionEnabled = true
        
        self.addSubview(self.bgImg)
        self.addSubview(self.labName)
        self.addSubview(self.labCompany)
        self.addSubview(self.imgUserIcon)
        self.addSubview(self.btnRight)
        
        setNeedsUpdateConstraints()
    }
    
    
    //MARK: - 查看用户信息
    @IBAction private func btnShowUserInfoAction(sender:UIButton) {
        self.showUserDetailsBlock?()
    }
    
    
    //MARK: - 点击登录
    @objc private func togoLogin(sender:Any) {
        self.gotoLoginBlock?()
    }
    
    
    //MARK: - lazy load
    private lazy var bgImg:UIImageView = {
        let _img = BaseView.createImage(rect: CGRect.zero,
                               image: YYImage.init(named: "mine_bg"),
                               backgroundColor: nil)
        return _img
    }()
    
    lazy var labName:YYLabel = {
       let _t = BaseView.createLable(rect: CGRect.zero,
                                     text: UserModel.userIsLogined ? UserModel.userName : "请登录",
                                     textColor: UIColor.init(hexString: "#333333")!,
                                     font: UIFont.systemFont(ofSize: 18, weight: .bold))
       
       _t.textAlignment = .left
       return _t
   }()
    
   lazy var labCompany:YYLabel = {
        let _t = BaseView.createLable(rect: CGRect.zero,
                                      text: UserModel.userIsLogined ? UserModel.userOrgIzaName :"xxx股份有限公司",
                                      textColor: UIColor.init(hexString: "#333333")!,
                                      font: UIFont.systemFont(ofSize: 15, weight: .bold))
        
        _t.textAlignment = .left
        return _t
    }()
    
    private lazy var imgUserIcon:UIImageView = {
        //圆角 byRoundCornerRadius(rect.size.height * 5)
        let _img = BaseView.createImage(rect: CGRect.zero,
                               image: YYImage.init(named: K_APP_DEFAULT_USER_IMAGE),
                               backgroundColor: nil)

        return _img
    }()
    
    private lazy var btnRight:UIButton = {
        let _b = BaseView.createBtn(rect: CGRect.zero,
                               strTitle: nil,
                               titleColor: nil,
                               txtFont: nil,
                               image: nil,
                               backgroundColor: nil,
                               borderColor: nil,
                               cornerRadius: 0,
                               isRadius: false,
                               backgroundImage: YYImage.init(named: "nav_arrow_right"),
                               borderWidth: 0)
        
        _b.addTarget(self, action: #selector(btnShowUserInfoAction(sender:)), for: .touchUpInside)
        return _b
    }()
}
